package com.java.poc.pdf;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.tool.xml.XMLWorkerHelper;

public class ConvertHTMLToPDF {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String pdfFilename = "d:\\temp1.pdf";
		  ConvertHTMLToPDF convertHTMLToPDF = new ConvertHTMLToPDF();
		  /*if (args.length < 1)
		  {
		   System.err.println("Usage: java "+ convertHTMLToPDF.getClass().getName()+
		   " PDF_Filename");
		   System.exit(1);
		  }*/

		  //pdfFilename = args[0].trim();
		  convertHTMLToPDF.createPDF(pdfFilename);
	}

	 private void createPDF (String pdfFilename){

		  //path for the PDF file to be generated
		  String path = pdfFilename;
		  PdfWriter pdfWriter = null;

		  //create a new document
		  Document document = new Document();

		  try {

		   //get Instance of the PDFWriter
		   pdfWriter = PdfWriter.getInstance(document, new FileOutputStream(path));

		   //document header attributes
		   document.addAuthor("betterThanZero");
		   document.addCreationDate();
		   document.addProducer();
		   document.addCreator("MySampleCode.com");
		   document.addTitle("Demo for iText XMLWorker");
		   document.setPageSize(PageSize.LETTER);

		   //open document
		   document.open();

		   //To convert a HTML file from the filesystem
		   //String File_To_Convert = "docs/SamplePDF.html";
		   //FileInputStream fis = new FileInputStream(File_To_Convert);

		   //URL for HTML page
		   URL myWebPage = new URL("http://www.tegrisfire.com/userLogin.action");
		   String File_To_Convert = "D:\\Java\\MyWorkspace\\Poc\\javapoc\\apppoc\\test1.htm";
	       //String url = new File(File_To_Convert).toURI().toURL().toString();
		   InputStreamReader fis = new InputStreamReader(myWebPage.openStream());
	       //InputStreamReader fis = new InputStreamReader((new File(File_To_Convert).toURI().toURL()).openStream());

		   //get the XMLWorkerHelper Instance
		   XMLWorkerHelper worker = XMLWorkerHelper.getInstance();
		   //convert to PDF
		   worker.parseXHtml(pdfWriter, document, fis);

		   //close the document
		   document.close();
		   //close the writer
		   pdfWriter.close();

		  }

		  catch (FileNotFoundException e) {
		   e.printStackTrace();
		  } catch (IOException e) {
		   e.printStackTrace();
		  } catch (DocumentException e) {
		   e.printStackTrace();
		  }

		 }

}
